package com.cheer.service.impl;

import cn.hutool.core.date.DateUtil;
import com.cheer.dao.OaIntegralDao;
import com.cheer.entity.ExMatchUserEntity;
import com.cheer.entity.OaIntegralEntity;
import com.cheer.entity.OaLearnEntity;
import com.cheer.enums.OaIntegralRuleEnum;
import com.cheer.service.*;
import com.cheer.vo.OaTaskInfoVO;
import com.cheer.vo.TaskInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.function.Supplier;

/**
 * @author Administrator
 */
@Service
public class TaskServiceImpl implements TaskService {


    public static final String[] tasksDescArray = {"今日登陆", "本期团课", "精彩图文", "阅读龙江", "积分换购",  "主题活动", "全部完成"};
    public static final String[] awardDescArray = {"+5积分", "+20积分", "+25积分", "+10积分", "+10积分", "+5积分", "抽奖5次" };

    @Autowired
    private OaIntegralDao oaIntegralDao;

    @Autowired
    private OaLearnService oaLearnService;

    @Autowired
    private CacheService cacheService;

    @Autowired
    private OaIntegralService oaIntegralService;

    @Override
    public List<TaskInfoVO> getTaskStatus(String openid, ExMatchUserEntity exMatchUserEntity) {

        Date date = new Date();

        List<TaskInfoVO> result = new ArrayList<>();
        // 登陆默认完成
        result.add( new TaskInfoVO(tasksDescArray[0], true,awardDescArray[0]));

        // 本期学习
        Supplier<Boolean> current = () -> {
            OaLearnEntity firstLearn = oaLearnService.getFirst();
            return oaIntegralDao.selectCountByItemsId(openid, OaIntegralRuleEnum.CURRENT.getCode(), firstLearn.getId()) > 0;
        };
        result.add(addTaskStatus(openid, 1, OaIntegralRuleEnum.CURRENT, current));

        // 精彩图文
        Supplier<Boolean>  article = () -> oaIntegralDao.selectCountByOpenid(openid, OaIntegralRuleEnum.ARTICLE_PIC.getCode(),
                DateUtil.beginOfDay(date), DateUtil.endOfDay(date)) >= OaIntegralRuleEnum.ARTICLE_PIC.getMax();
        result.add(addTaskStatus(openid, 2, OaIntegralRuleEnum.ARTICLE_PIC, article));

        // 阅读龙江
        Supplier<Boolean> readLJ = () -> oaIntegralDao.selectCountByOpenid(openid, OaIntegralRuleEnum.ARTICLE_WIN.getCode(),
                DateUtil.beginOfDay(date), DateUtil.endOfDay(date)) >= OaIntegralRuleEnum.ARTICLE_WIN.getMax();
        result.add(addTaskStatus(openid, 3, OaIntegralRuleEnum.ARTICLE_WIN, readLJ));

        // 积分换购
        Supplier<Boolean> past = () -> cacheService.taskIsFinished(openid, OaIntegralRuleEnum.EXCHANGE);
        result.add(addTaskStatus(openid, 4, OaIntegralRuleEnum.EXCHANGE, past));

        // 主题活动
        Supplier<Boolean> act = () -> cacheService.taskIsFinished(openid, OaIntegralRuleEnum.ACTIVITY);
        result.add(addTaskStatus(openid, 5, OaIntegralRuleEnum.ACTIVITY, act));

        // 全部完成
        Optional<TaskInfoVO> taskInfoVO = result.stream().filter(taskInfo -> taskInfo.getIsFinished().equals(Boolean.FALSE)).findAny();
        Boolean allTaskBoolean = taskInfoVO.isPresent();
        result.add( new TaskInfoVO(tasksDescArray[6], !allTaskBoolean,awardDescArray[6]) );

        // 首页弹窗 没提则不需要弄
        if(!allTaskBoolean){
            String homeTaskFlag = cacheService.getHomeTaskFlag(openid);
            if( homeTaskFlag == null){
                cacheService.setHomeTaskFlag("ableOpenTab","");
            }
        }
        return result;
    }

    /**
     * 拆成公共方法
     * @param openid
     * @param index
     * @param ruleEnum
     * @param supplier
     * @return
     */
    private TaskInfoVO addTaskStatus( String openid, Integer index,
                                      OaIntegralRuleEnum ruleEnum, Supplier<Boolean> supplier) {

        if(cacheService.taskIsFinished(openid, ruleEnum)){
            return new TaskInfoVO(tasksDescArray[index], true ,awardDescArray[index]);
        }else{
            Boolean readLJ = supplier.get();
            if(readLJ){
                cacheService.setTaskUserValue(openid, ruleEnum);
            }
            return new TaskInfoVO(tasksDescArray[index], readLJ,awardDescArray[index]);
        }

    }

    @Override
    public OaTaskInfoVO task(String openid, ExMatchUserEntity exUserByOpenId) {

        OaTaskInfoVO taskInfoVO = new OaTaskInfoVO();
        List<TaskInfoVO> taskStatus = getTaskStatus(openid, exUserByOpenId);
        List<Boolean> finishList = new ArrayList<>();
        taskStatus.forEach( task ->  finishList.add(task.getIsFinished()));
        // 获取今日积分
        List<OaIntegralEntity> integralDetailEntityList = oaIntegralDao.selectCountByToday(openid,
                DateUtil.beginOfDay(new Date()), (DateUtil.endOfDay(new Date())));
        int sum = 0;
        for (OaIntegralEntity integralDetailEntity : integralDetailEntityList) {
            sum += integralDetailEntity.getIntegral();
        }
        taskInfoVO.setCredit(sum);
        taskInfoVO.setFinishList(finishList);

        return taskInfoVO;
    }

    @Override
    public void execTask(Integer userId, String openid, OaIntegralRuleEnum activity) {
        Boolean isFinished = cacheService.taskIsFinished(openid, activity);
        if(isFinished == false){
            oaIntegralService.add(userId, openid, activity, null);
            cacheService.setTaskUserValue(openid, activity);
        }
    }
}
